package com.ruoyi.web.controller.api.webapp;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.house.service.WAppLoginService;
import com.ruoyi.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/***
 * @title WAppLoginController
 * @description web 应用登录api
 * @author WeiShuo
 * @version 1.0.0
 * @create 2024/6/18 16:06
 **/

@RequestMapping("/api/webapp/login")
@RestController
public class WAppLoginController {

    @Autowired
    private WAppLoginService wAppLoginService;

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 登录方法
     *
     * @param phoneNumber 登录信息
     * @return 结果
     */
    @PostMapping("/phone")
    public AjaxResult login(String  phoneNumber)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = wAppLoginService.phoneLogin(phoneNumber);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/user/getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

}
